from core.Process import Process
import queue


class Once:

    def __init__(self):
        self.process_list = queue.Queue()

        self.time_slice_first = 2
        self.time_slice_second = 4
        self.time_slice_third = 8

    def reset(self):
        self.process_list = queue.Queue()

        self.time_slice_first = 2
        self.time_slice_second = 4
        self.time_slice_third = 8

    def getPidCount(self):
        return self.process_list.qsize()

    def getTimeSlice(self):
        return self.time_slice_first, self.time_slice_second, self.time_slice_third

    def setTimeSlice(self, first, second, third):
        self.time_slice_first = int(first)
        self.time_slice_second = int(second)
        self.time_slice_third = int(third)

    def addProcess(self, arrival_time, work_time):
        process = Process(name=self.process_list.qsize(), receive_time=arrival_time, need_time=work_time)
        self.process_list.put(process)

    def getMission(self):
        return Mission([self.time_slice_first, self.time_slice_second, self.time_slice_third], self.process_list)
        pass
        # ui_worker = UIWorker()
        # ui_worker.start()
        # ui_worker.countChanged.connect(self.flush)
        # mission = Mission([self.time_slice_first, self.time_slice_second, self.time_slice_third], self.process_list)
        # currentTime = 0
        # while 1:
        #     time.sleep(1)
        #     showTime(currentTime)
        #     mission.run()
        #     # showQueue(
        #     #     init_list=list(mission.zero_level_queue.queue),
        #     #     first_list=list(mission.first_level_queue.queue),
        #     #     second_list=list(mission.second_level_queue.queue),
        #     #     third_list=list(mission.third_level_queue.queue)
        #     # )
        #     currentTime += 1
        #     if mission.check() == 0:
        #         break
        # print("finish")


class Mission:
    def __init__(self, slide_size, zero_level_queue):
        self.slide_size = slide_size
        self.first_level_energy = slide_size[0]
        self.second_level_energy = slide_size[1]
        self.third_level_energy = slide_size[2]
        self.now_time = 0
        self.zero_level_queue = zero_level_queue
        self.first_level_queue = queue.Queue()
        self.second_level_queue = queue.Queue()
        self.third_level_queue = queue.Queue()
        self.finish_queue = queue.Queue()

    def check(self):
        if self.zero_level_queue.qsize() == 0 and self.first_level_queue.qsize() == 0 and self.second_level_queue.qsize() == 0 and self.third_level_queue.qsize() == 0:
            return 0

    def show_queue(self):
        queue_list = list(self.zero_level_queue.queue)
        print("等待队列:")
        for item in queue_list:
            # 访问元素的属性并输出内容
            print(item.name, item.receive_time, item.need_time, item.rest_time)
        queue_list = list(self.first_level_queue.queue)
        print("一级队列:")
        for item in queue_list:
            # 访问元素的属性并输出内容
            print(item.name, item.receive_time, item.need_time, item.rest_time)
        queue_list = list(self.second_level_queue.queue)
        print("二级队列:")
        for item in queue_list:
            # 访问元素的属性并输出内容
            print(item.name, item.receive_time, item.need_time, item.rest_time)
        queue_list = list(self.third_level_queue.queue)
        print("三级队列:")
        for item in queue_list:
            # 访问元素的属性并输出内容
            print(item.name, item.receive_time, item.need_time, item.rest_time)
        queue_list = list(self.finish_queue.queue)
        print("结束队列:")
        for item in queue_list:
            # 访问元素的属性并输出内容
            print(item.name, item.receive_time, item.need_time, item.rest_time, item.end_time)

    def run(self):
        self.now_time += 1
        for i in range(self.zero_level_queue.qsize()):
            # 检查所有等待队列的进程
            t = self.zero_level_queue.queue[0]
            if t.receive_time <= self.now_time:
                # 如果时间到了, 则把他放入第一队列
                self.first_level_queue.put(self.zero_level_queue.get())
        if self.first_level_queue.qsize() != 0:
            # 若不为空则检查第一队列

            self.first_level_queue.queue[0].rest_time -= 1  # 所需时间减一
            self.first_level_energy -= 1
            if self.first_level_queue.queue[0].rest_time == 0:
                # 第一队列头运行结束
                self.first_level_energy = self.slide_size[0]  # 重新计算时间片
                self.first_level_queue.queue[0].end_time = self.now_time  # 记录结束时间
                self.finish_queue.put(self.first_level_queue.get())  # 从队列中删除
            if self.first_level_energy == 0:
                # 第一队列头时间片用完
                self.first_level_energy == self.slide_size[0]  # 重新计算时间片
                self.second_level_queue.put(self.first_level_queue.get())  # 放入第二队列
        elif self.second_level_queue.qsize() != 0:
            # 第一队列为空且第二对列不为空

            self.second_level_queue.queue[0].rest_time -= 1  # 所需时间减一
            self.second_level_energy -= 1
            if self.second_level_queue.queue[0].rest_time == 0:
                # 第二 队列头运行结束
                self.second_level_energy = self.slide_size[0]  # 重新计算时间片
                self.second_level_queue.queue[0].end_time = self.now_time  # 记录结束时间
                self.finish_queue.put(self.second_level_queue.get())  # 从队列中删除
            if self.second_level_energy == 0:
                # 第二 队列头时间片用完
                self.second_level_energy == self.slide_size[1]  # 重新计算时间片
                self.third_level_queue.put(self.second_level_queue.get())  # 放入第二队列
        elif self.third_level_queue.qsize() != 0:
            # 仅剩第三队列不为空
            self.third_level_queue.queue[0].rest_time -= 1  # 所需时间减一
            self.third_level_energy -= 1
            if self.third_level_queue.queue[0].rest_time == 0:
                # 第三 队列头运行结束
                self.third_level_energy = self.slide_size[0]  # 重新计算时间片
                self.third_level_queue.queue[0].end_time = self.now_time  # 记录结束时间
                self.finish_queue.put(self.third_level_queue.get())  # 从队列中删除
            if self.third_level_energy == 0:
                # 第三 队列头时间片用完
                self.third_level_energy == self.slide_size[2]  # 重新计算时间片
                self.third_level_queue.put(self.third_level_queue.get())
        print("第%d秒结束时:" % self.now_time)
